﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using DigDes.DS2012.BusinessObjects;

namespace DigDes.DS2012.WebUI
{
    public class Global : System.Web.HttpApplication
    {
        private const string SESSION_KEY_APP_USER = "AppUser";
        private const string SESSION_KEY_GLOBAL_CONTEXT = "GlobalContext";
        private object syncRoot = new object();

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Текущий пользователь системы
        /// </summary>
        public static AppUser AppUser
        {
            get
            {
                if (HttpContext.Current != null)
                    return (AppUser)HttpContext.Current.Session[SESSION_KEY_APP_USER];
                else
                    return null;
            }
        }

        /// <summary>
        /// Текущий контекст приложения
        /// </summary>
        public static IGlobalContext GlobalContext
        {
            get
            {
                return (IGlobalContext)HttpContext.Current.Session[SESSION_KEY_GLOBAL_CONTEXT];
            }
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            lock (syncRoot)
            {
                Session[SESSION_KEY_GLOBAL_CONTEXT] = new GlobalContext();
                Session[SESSION_KEY_APP_USER] = new AppUser(GlobalContext);
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}